﻿using System;
using System.Windows;
using System.Windows.Controls;

using SBPweb.PROvibe.Shared.Actors;

namespace SBPweb.PROvibe.Client.Framework.Controls
{

    public partial class SmartpartContainer : ContentControl
    {

        #region Private fields

        private IPage parentPage;
        private IContentManager contentManager;
        private Grid gridMain;
        private TextBlock tbTitle;
        private Grid gridBody;

        #endregion

        #region Constructor logic

        public SmartpartContainer()
        {
            DefaultStyleKey = typeof(SmartpartContainer);
            InitializeComponent();
        }

        #endregion

        #region Property accessors

        public int? SmartpartId
        {
            get;
            set;
        }

        public string SmartpartType
        {
            get;
            set;
        }

        public IContentManager ContentManager
        {
            get
            {
                return contentManager;
            }
            set
            {
                contentManager = value;
            }
        }

        public IPage ParentPage
        {
            get
            {
                return parentPage;
            }
            set
            {
                parentPage = value;
            }
        }

        #endregion

        #region Methods
                
        public void LoadSmartpart()
        {
            LoadSmartpart(ContentManager, ParentPage, SmartpartId, SmartpartType);
        }

        public void LoadSmartpart(IContentManager contentManager, IPage parentPage)
        {
            LoadSmartpart(contentManager, parentPage, SmartpartId, SmartpartType);
        }

        public void LoadSmartpart(IContentManager contentManager, IPage parentPage, int smartpartId)
        {
            LoadSmartpart(contentManager, parentPage, smartpartId, null);
        }

        public void LoadSmartpart(IContentManager contentManager, IPage parentPage, string smartpartType)
        {
            LoadSmartpart(contentManager, parentPage, null, smartpartType);
        }

        public void LoadSmartpart(IContentManager contentManager, IPage parentPage, int? smartpartId, string smartpartType)
        {
            this.ContentManager = contentManager;
            this.ParentPage = parentPage;
            this.SmartpartId = smartpartId;
            this.SmartpartType = smartpartType;
            if(smartpartId.HasValue)
            {
                contentManager.LoadSmartpart(smartpartId.Value, new EventHandler<TypedObjectEventArgs<ISmartpart>>(OnSmartpartInstanceLoaded), parentPage);
            }
            else if (!string.IsNullOrEmpty(smartpartType))
            {
                contentManager.LoadSmartpart(new TypeDescriptor(smartpartType), new EventHandler<TypedObjectEventArgs<ISmartpart>>(OnSmartpartInstanceLoaded), parentPage);
            }
            else
            {
                throw new ArgumentNullException("Type could not be created!");
            }
        }

        #region Event handlers

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            gridBody = GetTemplateChild("SmartpartContainerBody") as Grid;
            tbTitle = GetTemplateChild("SmartpartContainerTBTitle") as TextBlock;
        }

        #endregion

        private void OnSmartpartInstanceLoaded(object sender, TypedObjectEventArgs<ISmartpart> e)
        {
            ISmartpart sp = e.Object;
            UIElement uie = sp as UIElement;
            tbTitle.Text = string.Format("..:: {0} ::..", sp.Caption);
            gBody.Children.Add(uie);
        }

        #endregion

    }

}